package com.iplay.实战java高并发程序设计.chapter3.c31;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author dangwu
 * @since 2021/4/30 6:11 下午
 * 测试reentrantLock 如果添加 static，那么线程参数对象可以是任意对象
 */
public class ReenterLock implements Runnable{
    public static ReentrantLock lock = new ReentrantLock();
    private static int n = 0;
    @Override
    public void run() {
        for (int i = 0; i < 1000000; i++) {
            lock.lock();
            try {
                n++;
            }finally {
                lock.unlock();
            }
        }
    }
    static Pattern compile = Pattern.compile("\\d+");

    public static void main(String[] args) throws InterruptedException {

//        for (int i = 0; i < 10; i++) {
//            Thread t1 = new Thread(new ReenterLock());
//            Thread t2 = new Thread(new ReenterLock());
//            t1.start();
//            t2.start();
//            t1.join();
//            t2.join();
//            if (n != 2000000){
//                System.out.println(n);
//            }else {
//                n = 0;
//            }
//
//        }
//        String s = "123";
//        Matcher matcher = compile.matcher(s);
//        if (matcher.matches()){
//            System.out.print("1");
//        }else {
//            System.out.println(0);
//        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 123; i++) {
            list.add(i);
        }
        int num = 10;
        int size = list.size()%num == 0 ? list.size()/num : list.size()/num +1;
        System.out.println("list.size="+list.size()+" c="+size);
        for (int i = 0; i < size; i++) {
            if (list.size()/num == i){
                List<Integer> integers = list.subList(num * i, list.size());
                System.out.println(JSON.toJSONString(integers));
            }else {
                List<Integer> integers = list.subList(num * i, num * (i + 1));
                System.out.println(JSON.toJSONString(integers));
            }

        }
    }
}
